package com.zgg;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.MultimediaInfo;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * User:zgg<gang@eventslack.com>
 *
 * @date 2020/5/11 16:26
 * 用来通过url获得视频的时长
 */
public class VideoUtils {

    /**
     * 将url的资源统一转换成字节数组
     *
     * @param urlStr
     * @return
     * @throws IOException
     */
    public static byte[] toByteArray(String urlStr) throws IOException {
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        DataInputStream in = new DataInputStream(conn.getInputStream());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024 * 4];
        int n = 0;
        while ((n = in.read(buffer)) != -1) {
            out.write(buffer, 0, n);
        }
        return out.toByteArray();
    }

    /**
     * 将输入流
     *
     * @param in
     * @return
     * @throws IOException
     */
    public static byte[] toByteArray(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024 * 4];
        int n = 0;
        while ((n = in.read(buffer)) != -1) {
            out.write(buffer, 0, n);
        }
        return out.toByteArray();
    }


    /**
     * 通过url获得文件保存到临时的文件
     *
     * @param url
     * @return
     * @throws IOException
     */
    public static File getFileByUrl(String url) throws IOException {
        DataInputStream in = null;
        FileOutputStream out = null;
        try {
            //创建临时文件用来放url的文件
            File file = File.createTempFile("vedio", ".tmp");
            //通过url获得相应资源
            URL videoUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) videoUrl.openConnection();
            in = new DataInputStream(connection.getInputStream());
            //将数据输入流转换成字节
            byte[] data = toByteArray(in);
            //用输出流将字节输出成文件
            out = new FileOutputStream(file.getCanonicalPath());
            out.write(data);
            return file;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭相应的资源
            in.close();
            out.close();
        }
        return null;
    }

    /**
     * 通过类获得视频时长 单位毫秒
     *
     * @param file
     * @return
     * @throws EncoderException
     */
    public static long getDuration(File file) throws EncoderException {
        MultimediaInfo m = new Encoder().getInfo(file);
        return m.getDuration();
    }


    public static void main(String[] args) {
        try {
            File file = getFileByUrl("http://file.bagevent.com/devOps2.mp4");
            Long duration = getDuration(file) / 1000;
            System.out.println("时长：" + duration + "秒");
        } catch (IOException | EncoderException e) {
            e.printStackTrace();
        }

    }

}
